package com.uuah.server.session;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.server.api.IUuahMessage;
import com.uuah.server.api.session.ISessionHandler;
import com.uuah.server.api.session.IUuahSession;
import com.uuah.server.exception.AbstractUuahServerException;
import com.uuah.server.transport.PropertyScope;

public class UuahSessionHandler implements ISessionHandler {

	protected transient Logger logger = LoggerFactory.getLogger(getClass());

	public void retrieveSessionInfoFromMessage(IUuahMessage message,
			IUuahSession session) throws AbstractUuahServerException {
		Map sessionMap = message.getAdapter().getScopeProperties(
				PropertyScope.SESSION);
		Set keys = sessionMap.keySet();
		for (Object key : keys) {
			session.setProperty(key, sessionMap.get(key));
		}
	}

	public void storeSessionInfoToMessage(IUuahSession session,
			IUuahMessage message) throws AbstractUuahServerException {
		for (Iterator iterator = session.getPropertyNames(); iterator.hasNext();) {
			Object o = iterator.next();
			message.setProperty((String) o, session.getProperty(o),
					PropertyScope.SESSION);
		}
	}

	public String getSessionIDKey() {
		return "ID";
	}
}
